<template>
  <div class="execution">
    <dynamic-crud
      :rowAdd="rowadd"
      :data="tableDataEle"
      :option="tableOptionEle"
      title="电子支付"
      :isShow="oneShow"
      :rowOneButton="oneButton"
      :addNew="rowNew"
      :hiddened="myType == '1'"
      :addDisabled="myType == '1' && tableDataEle.length > 0"
    ></dynamic-crud>
    <!-- <dynamic-crud
      :rowAdd="rowadd"
      :data="tableDataEle"
      :option="tableOptionEle"
      title="电子支付"
      :addDisabled="myType == '1' && tableDataEle.length > 0"
    ></dynamic-crud> -->
    <div class="bottom-menu">
      <el-button type="primary" :disabled="btn" @click="save">保存</el-button>
      <el-button type="primary" @click="closeDia()">取消</el-button>
    </div>
  </div>
</template>

<script>
import {
  saveObj,
  getTvmCode
} from "@/api/subwaystm/swsttvmdailybill/swsttvmdailybill";
import { tableOptionEle } from "@/const/crud/subwaystm/swsttvmdailybill/swsttvmdailybillelectronicpay";
import { mapGetters } from "vuex";
import store from "@/store";
import { dateFormat } from "@/util/date";
export default {
  name: "swsttvmdailybillelectronicpay",
  inject: ["closeDialog"],
  data() {
    return {
      tableDataEle: [],
      tableLoading: false,
      //tableOptionEle: tableOptionEle,
      oneShow: true,
      rowNew: true,
      tvmList: [],
      //tableOptionEle: tableOptionEle,
      btn: false,
      formAdd: {
        swStTvmDailyBill: {},
        swStTvmDailyBillElectronicPays: []
      }
    };
  },
  computed: {
    tableOptionEle() {
      let option = this.deepClone(tableOptionEle);
      this.findObject(option.column, "tvmCode").cell = this.myType == "0";
      if (this.oneShow && this.myType == "0") {
        this.findObject(option.column, "tvmCode").cell = false;
      }
      return option;
    }
  },
  props: {
    eleData: {
      type: Array,
      default: function() {
        return [];
      }
    },
    myType: String,
    tvmCode: String
  },
  watch: {
    eleData() {
      this.tableDataEle = this.eleData;
    }
  },

  beforeMount() {
    getTvmCode().then(res => {
      this.tvmList = res.data.data.records;
    });
  },
  methods: {
    rowadd: function(callback) {
      this.oneShow = false;
      this.tableOptionEle.column[1].cell = true;
      let startTime = new Date();
      let endTime = new Date();
      if (startTime.getHours() < 6) {
        startTime.setDate(startTime.getDate() - 1);
      }
      if (endTime.getHours() >= 6) {
        endTime.setDate(endTime.getDate() + 1);
      }
      if (this.tableDataEle.length > 0) {
        callback({
          startTime: dateFormat(startTime).substr(0, 10) + " 02:00:00",
          endTime: dateFormat(endTime).substr(0, 10) + " 01:59:59"
          // startTime:this.tableDataEle[0].startTime,
          // endTime:this.tableDataEle[0].endTime
        });
      } else {
        if (this.myType == "0") {
          callback({
            startTime: dateFormat(startTime).substr(0, 10) + " 02:00:00",
            endTime: dateFormat(endTime).substr(0, 10) + " 01:59:59"
          });
        } else {
          callback({
            tvmCode: this.tvmCode,
            startTime: dateFormat(startTime).substr(0, 10) + " 02:00:00",
            endTime: dateFormat(endTime).substr(0, 10) + " 01:59:59"
          });
        }
      }
    },
    oneButton: function(callback) {
      this.rowNew = false;
      this.tableOptionEle.column[1].cell = false;
      if (this.tableDataEle.length > 0) {
        this.$confirm("此操作将重新生成, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          console.log(this.tableDataCashThis);
          for (const item of this.tableDataEle) {
            Object.keys(item).map(key => {
              if (key != "$cellEdit" && key != "tvmCode") {
                item[key] = undefined;
              }
            });
          }
        });
      } else {
        for (const item of this.tvmList) {
          const obj = Object.assign({}, item);
          let startTime = new Date();
          let endTime = new Date();
          if (startTime.getHours() < 6) {
            startTime.setDate(startTime.getDate() - 1);
          }
          if (endTime.getHours() >= 6) {
            endTime.setDate(endTime.getDate() + 1);
          }
          (obj.startTime = dateFormat(startTime).substr(0, 10) + " 02:00:00"),
            (obj.endTime = dateFormat(endTime).substr(0, 10) + " 01:59:59");
          callback(obj);
        }
      }
    },
    save: function() {
      let tvmCodeList = [];
      if (this.tableDataEle.length == 0) {
        this.$message.info("添加电子支付录入信息！");
        return;
      } else {
        for (const item of this.tableDataEle) {
          tvmCodeList.push(item.tvmCode);
          if (
            item.tvmCode == null ||
            item.rechargeMoney == null ||
            item.sellTicketMoney == null ||
            item.startTime == null ||
            item.endTime == null
          ) {
            this.$message.info(
              "TVM编号、售票总计、充值总计、开始时间、结束时间为必填项！不能为空"
            );
            return;
          }
          if(item.sellTicketMoney!==undefined){
            if(String(item.sellTicketMoney).length>4){
              this.$message.info(
                "请检查售票总计金额不能超过4位数！"
              );
              return;
            }
          }
          if(item.rechargeMoney!==undefined){
            if(String(item.rechargeMoney).length>4){
              this.$message.info(
                "请检查充值总计金额不能超过4位数！"
              );
              return;
            }
          }
        }
        var s = tvmCodeList.join(",")+",";
        for(var i=0;i<tvmCodeList.length;i++){
          if (s.replace(tvmCodeList[i]+",","").indexOf(tvmCodeList[i]+",")>-1){
            this.$message.info(
              "请检查新增的数据中不能有相同TVM编号的数据！"
            );
            return;
          }
        }
      }
      this.formAdd.swStTvmDailyBillElectronicPays = this.tableDataEle;
      this.formAdd.swStTvmDailyBill.startTime = this.tableDataEle[0].startTime;
      this.formAdd.swStTvmDailyBill.endTime = this.tableDataEle[0].endTime;
      this.formAdd.swStTvmDailyBill.operatorName =
        store.getters.userInfo.nikeName;
      this.formAdd.swStTvmDailyBill.operatorJobNo =
        store.getters.userInfo.userCode;
      saveObj(this.formAdd)
        .then(data => {
          this.$message.success("编辑成功");
          this.btn = true;
          this.closeDialog();
          done();
        })
        .catch(() => {});
    },
    closeDia() {
      this.$confirm("请确认是否已将数据保存?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.closeDialog();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.bottom-menu {
  margin-top: 48px;
  text-align: center;
}
</style>
